new Vue({
    el:'#set-user-limit',
    data:{
        tHead:[
            {key:'id',name:'id'},
            {key:'userId',name:'用户id'}, //从table中选择的每条数据的id
            {key:'userName',name:'用户名'},
            {key:'userDesc',name:'用户描述'},                            
        ], 
        userList:[{id:'',userId:'',userName:''}],
        field:['id','userId', 'userName', 'userDesc'],  //表格中的所有字段
        fields:{         //点击表格中的一行，选择数据时，动态传递需要的字段
            f1:'userId',
            f2:'userName'
        },
        pnum:1,
        psize:5,
        tnum:0,
        tableList:[],
        show:true
    },
    methods:{      
        //添加用户
        addUser:function(){
            this.userList.push({id:'',userId:'',userName:''});
            $(".table").hide();
        },

        //进行保存操作                                
        save:function(){
            var result  = this.userList;
            var isReapeat = '';
            if(result != []){
                isReapeat = unique(result,this.fields.f2);
                if(isReapeat != false){
                    var _this = this;
                    $.ajax({
                        type:'post',
                        url:'/actField/auth/setUser',
                        dataType:'json',
                        contentType:'application/json',
                        data:JSON.stringify({
                            fieldId: getParams().id,
                            auths:result
                        }),
                        success:function(res){
                            if(res.success == true){                                                
                                layer.msg('保存成功',{
                                    time:1000,
                                    end:function(){
                                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                        parent.layer.close(index); //再执行关闭
                                    }
                                })
                            }else{
                                alert('请求失败');
                            }
                        }
                    });                        
                }                               
                      
            }else{
                alert('请填写数据之后，再保存');
            }                            
        },

        //从子组件接收数据，进行删除操作
        del:function(){
            loadLayer = layer.load(1, {
                shade: [0.3,'#000']
            });        
            var _this = this;
            var data = this.userList;
            var checkedIds = [];  
            var checkedKeys = [];    
            var checkedLen = 1;      
            for(var i=0;i<data.length;i++){
                if(data[i].checked == true){
                    checkedLen++;
                    checkedIds.push(data[i].id);
                    checkedKeys.push(i);
                }
            }
            if(checkedLen > 1){               
                $.ajax({
                    type:'post',
                    url:'/actField/auth/delUser',
                    dataType:'json',
                    contentType:'application/json',
                    data:JSON.stringify(checkedIds),
                    success:function(res){
                        if(res.success == true){     
                            layer.msg('删除成功',{
                                time:1000,
                                end:function(){                                                
                                    for(var i=0;i<checkedKeys.length;i++){
                                        data[checkedKeys[i]] = null;
                                    }
                                    //删除掉null,得到新数组
                                    var numArr = [];
                                    for(var i=0;i<data.length;i++){
                                        if(data[i] === null){
                                            numArr.push(i);
                                        }
                                    }
                                    numArr.sort(function(a,b){
                                        return a < b;
                                    });
                                    for(var i=0;i<numArr.length;i++){
                                        data.splice(numArr[i],1);
                                    }                                
                                    
                                    this.userList = data;                                                    
                                    layer.close(loadLayer);                      
                                }
                            });                    
                        }else{
                            alert('请求失败');
                            layer.close(loadLayer);    
                        }
                    }
                });
            }else{
                layer.msg('请选择要删除的数据！',{time:1000});
                layer.close(loadLayer);    
            }      
        },

        //通过接口得到userList的值
        getUserList:function(){
            var _this = this;
            $.ajax({
                type:'post',
                url:'/actField/auth/user/' + getParams().id,
                success:function(res){
                    if(res.success == true){
                        //根据enableAuth字段，判断是否显示添加用户和保存按钮。
                        if(res.data.enableAuth == 1){
                            $("#isEnableAuth").css("display","block");
                        }else{
                            $("#isEnableAuth").css("display","none");
                        }
                        var auths = res.data.auths;    
                        if(auths.length > 0){                           
                            _this.userList = auths;
                        } 
                    }else{
                        alert("请求失败");
                    }
                }
            }); 
        },

        //分页组件
        toPage:function(pageNo,qname){
            var _this = this;
            $.ajax({
                type:'post',
                url:'/manager/person/list',
                dataType:'json',
                contentType:'application/json',
                data:JSON.stringify({
                    pager: {
                        pageNum: pageNo, 
                        pageSize: _this.psize, 
                        totalNum: _this.tnum
                    }, 
                    queryModel: {
                        userName: qname, 
                        common: ''
                    }
                }),
                success:function(res){
                    if(res.success == true){
                        var data = res.data.datas;
                        _this.tableList = data;                        
                        var pager = res.data.pager;
                        _this.pnum = pager.pageNum;
                        _this.tnum = pager.totalNum;
                    }else{
                        alert("请求失败");
                    }
                }
            });
        }
    },
    mounted:function(){        
        this.getUserList();
    }
})



